<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<form onsubmit="return false">
    <textarea name="message" style="height: 300px; width: 300px"></textarea>
    <input type="button" value="发生消息" onclick="send(this.form.message.value)">
    <textarea id="responseText" style="height: 300px; width: 300px"></textarea>
    <input type="button" value="清空内容" onclick="document.getElementById('responseText').value=''">
</form>

<script>
    var socket;

    if (window.WebSocket){
        socket = new WebSocket("ws://192.168.0.56:7004/hello");
        //相当于channelRead0
        socket.onmessage = function (ev) {
            var rt = document.getElementById("responseText");
            rt.value = rt.value + "\n" + ev.data;

        }
        //感知连接(开启连接)
        socket.onopen = function (ev){
            var rt = document.getElementById("responseText");
            rt.value = "LinkStart.......";
        }
        //感知关闭
        socket.onclose = function (ev){
            var rt = document.getElementById("responseText");
            rt.value = rt.value +"\n"+"连接断开，又剩下孤独的一个人了呢~";
        }
    }else {
        alert("升级浏览器吧，傻屌")
    }

    function send(message){
        if (!window.socket){
            return;
        }
        if (socket.readyState === WebSocket.OPEN){
            //发送消息
            socket.send(message);
        }else {
            alert("连接未开启.....")
        }
    }
</script>
</body>
</html>